<script setup lang="ts">
import { getHomeGoodsGuessLikeAPI } from '@/api/home';
import type { PageParams } from '@/types/global';
import type { GuessItem } from '@/types/home';
import { onMounted, ref } from 'vue';

const finish = ref(false)
const list = ref<GuessItem[]>([])
// ts类型限制，限定PageParams对象里的属性必填
const pageParams: Required<PageParams> = {
  page: 1,
  pageSize: 10
}
const getGuessLikeData = async ()=>{
  if(finish.value === true){
    uni.showToast({icon: 'none', title: '没有更多数据了...'})
    return
  }
  const res = await getHomeGoodsGuessLikeAPI(pageParams)
  list.value.push(...res.result.items)
  if(pageParams.page < res.result.pages){
    pageParams.page++
  }else{
    finish.value = true
  }
}
const resetData = () => {
  // 重置页码
  pageParams.page = 1
  // 重置列表
  list.value = []
  // 重置结束标记
  finish.value = false
}
defineExpose({
  // 自定义方法名称
  resetData,
  getMore: getGuessLikeData
})
onMounted(()=>{
  getGuessLikeData()
})
</script>

<template>
  <!-- 猜你喜欢 -->
  <view class="caption">
    <text class="text">猜你喜欢</text>
  </view>
  <view class="guess">
    <navigator
      class="guess-item"
      v-for="item in list"
      :key="item.id"
      :url="`/pages/goods/goods?id=${item.id}`"
    >
      <image
        class="image"
        mode="aspectFill"
        :src="item.picture"
      ></image>
      <view class="name"> {{item.name}} </view>
      <view class="price">
        <text class="small">¥</text>
        <text>{{item.price}}</text>
      </view>
    </navigator>
  </view>
  <view class="loading-text"> {{ finish ? '没有更多数据了' : '正在加载...' }} </view>
</template>

<style lang="scss">
@import '@/components/styles/FnGuess.scss';
:host {
  display: block;
}

</style>